package com.github.niefy.modules.wx.testController;

import com.github.niefy.modules.third.form.ThirdUserForm;
import com.github.niefy.modules.wx.bo.ThirdSubMsg;
import com.github.niefy.modules.wx.service.SubTemplateMsgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Email;
import java.io.IOException;
import java.util.HashMap;

/**
 * 第三方业务信息查询转发
 */
@RestController
@RequestMapping("/wx/submsg")
@Tag(name = "测试")
@RequiredArgsConstructor
@Slf4j
@Validated
public class ThirdSubMsgTestController {

    private final WxMpService wxMpService;
    @Autowired
    SubTemplateMsgService subTemplateMsgService;

    /*
    *发送测试订阅消息
    * */
    @GetMapping("/testgetsend")
    @Operation(summary = "发送",description = "")
    public void getThirdToken(HttpServletRequest request, HttpServletResponse response,
                              @RequestParam(name = "val", required = false) @Email(message = "youxiianggg") String val) throws IOException, InterruptedException {

        String appid = "wxca53f8ae5bd03da5";
        var dataMap = new HashMap<String, String>();
        dataMap.put("thing1", "大象");
        dataMap.put("thing2", "测试消息");
        var msg = WxMpSubscribeMessage.builder().toUser("oHwMn6gjVy0ZuXBuGp0rVMYifnwQ")
                .templateId("xpalM_l22wC4jBGy1VYJpshNOc-RzS4YdxK7h4jzw2M")
                .page("https://www.modb.pro/db/183171")
                .title(appid)
                .dataMap(dataMap)
                .build();
        var thirdSubMsg = ThirdSubMsg.builder().appid(appid)
                .thirdId("123456")
                .msg(msg)
                .build();
        subTemplateMsgService.sendSubTemplateMsg(thirdSubMsg);

    }

    /*
     *测试校验
     * */
    @GetMapping("/test/{pathVal}")
    @Operation(summary = "发送", description = "")
    public void pathValid(HttpServletRequest request, HttpServletResponse response,
                              @RequestParam(name = "val", required = true) @Email(message = "url参数错误") String val) throws IOException, InterruptedException {

        String appid = "wxca53f8ae5bd03da5";

    }

    /*
     *测试校验
     * */
    @PostMapping("/test/jsonValid")
    @Operation(summary = "发送", description = "")
    public void pathValid(HttpServletRequest request, HttpServletResponse response,
                          @RequestBody @Validated ThirdUserForm val) throws IOException, InterruptedException {

        String appid = "wxca53f8ae5bd03da5";

    }
}
